package cn.b.sky.mes.cm.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.cm.dao.CalendarDao;
import cn.b.sky.mes.cm.model.Calendar;
import cn.b.sky.mes.cm.model.CalendarDay;
import cn.b.sky.mes.cm.model.WorkCenterCalendarDay;
import cn.b.sky.mes.cm.model.WorkUnitCalendarDay;
import cn.b.sky.mes.cm.model.condition.CalendarCondition;
import cn.b.sky.mes.cm.service.CalendarService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("calendarService")
public class CalendarServiceImpl implements CalendarService {

  private static final Logger LOGGER = LoggerFactory.getLogger(CalendarServiceImpl.class);

  @Resource
  CalendarDao calendarDao;

  @Override
  public List<Calendar> selectCalendarByCondition(CalendarCondition calendarCondition) {
    return calendarDao.selectCalendarByCondition(calendarCondition);
  }

  @Override
  public int selectCalendarCount(CalendarCondition calendarCondition) {
    return calendarDao.selectCalendarCount(calendarCondition);
  }

  @Override
  public Calendar selectCalendarByCode(String siteCode, String calendarCode) {
    return calendarDao.selectCalendarByCode(siteCode, calendarCode);
  }

  @Override
  public List<CalendarDay> selectCalendarDaysByCalendarCode(String siteCode, String calendarCode) {
    return calendarDao.selectCalendarDaysByCalendarCode(siteCode, calendarCode);
  }

  @Override
  public void insertCalendar(Calendar calendar) {
    List<CalendarDay> days = calendar.getCalendarDays();
    if (days != null && days.size() > 0) {
      Collections.sort(days);
      calendar.setStartDate(days.get(0).getCalendarDate());
      calendar.setEndDate(days.get(days.size() - 1).getCalendarDate());
    } else {
      calendar.setStartDate(null);
      calendar.setEndDate(null);
    }
    calendarDao.insertCalendar(calendar);
    if (calendar.getCalendarDays() != null) {
      for (CalendarDay day : calendar.getCalendarDays()) {
        calendarDao.insertCalendarDay(day);
      }
    }

  }

  @Override
  public void updateCalendar(Calendar calendar) {
    List<CalendarDay> days = calendar.getCalendarDays();
    if (days != null && days.size() > 0) {
      Collections.sort(days);
      calendar.setStartDate(days.get(0).getCalendarDate());
      calendar.setEndDate(days.get(days.size() - 1).getCalendarDate());
    } else {
      calendar.setStartDate(null);
      calendar.setEndDate(null);
    }
    calendarDao.updateCalendar(calendar);
    calendarDao.deleteCalendarDay(calendar.getSiteCode(), calendar.getCalendarCode());
    if (calendar.getCalendarDays() != null) {
      for (CalendarDay day : days) {
        calendarDao.insertCalendarDay(day);
      }
    }

  }

  @Override
  public BatchJobReport deleteCalendar(String siteCode, String calendarCode) {
    String cause = "";
    Boolean success = true;
    try {
      calendarDao.deleteCalendarDay(siteCode, calendarCode);
      calendarDao.deleteCalendar(siteCode, calendarCode);
    } catch (Exception e) {
      LOGGER.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        // 外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(calendarCode, cause, success);
  }

  @Override
  public List<Calendar> selectAllUnlockCalendars(String siteCode) {

    return calendarDao.selectAllUnlockCalendars(siteCode);
  }

  @Override
  public List<CalendarDay> selectCalendarDaysByCalendarCodeAndPeriod(String siteCode, String calendarCode,
      String startDate, String endDate) {
    return calendarDao.selectCalendarDaysByCalendarCodeAndPeriod(siteCode, calendarCode, startDate, endDate);
  }

  @Override
  public List<WorkCenterCalendarDay> selectWorkCenterCalendarDays(String siteCode, String[] workCenterCodes,
      String startDate, String endDate) {
    return calendarDao.selectWorkCenterCalendarDays(siteCode, workCenterCodes, startDate, endDate);
  }

  @Override
  public List<WorkUnitCalendarDay> selectWorkUnitCalendarDays(String siteCode, String[] workCenterCodes,
      String startDate, String endDate) {
    return calendarDao.selectWorkUnitCalendarDays(siteCode, workCenterCodes, startDate, endDate);
  }

}
